﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Bowerbird.Website.Models;

namespace System.Web.Mvc
{
    public static class HtmlHelperExtensions
    {
        public static HtmlString UserContactData(this HtmlHelper helper, ContactDataModel contactData)
        {
            StringBuilder html = new StringBuilder();

            string link = string.Empty;

            switch (contactData.ContactDataType)
            {
                case "phone":
                    {
                        link = String.Format(@"<a href=""callto:{0}"">{1}</a>", RemoveNonNumericChars(contactData.Value), contactData.Value);
                    } break;
                case "email":
                    {
                        link = String.Format(@"<a href=""mailto:{0}"">{0}</a>", contactData.Value);
                    } break;
                case "address":
                    {
                        link = String.Format(@"<a href=""http://maps.google.com/?q={0}"">{0}</a>", contactData.Value);
                    } break;
                default: break;
            }

            var removeLink = VirtualPathUtility.ToAbsolute(String.Format("~/contactdata/remove/{0}", contactData.Id));

            html.AppendFormat(@"{0} {1}: {2} | Visible to: <a href=""#"">{3}</a> | <a href=""{4}"">Remove</a>", contactData.Name, contactData.ContactDataType.ToString(), link, FormatVisibility(contactData.VisibleTo), removeLink);

            return new HtmlString(html.ToString());
        }

        public static HtmlString SuggestedContactData(this HtmlHelper helper, UserModel suggestedContact)
        {
            StringBuilder html = new StringBuilder();

            var profileLink = VirtualPathUtility.ToAbsolute(String.Format("~/user/profile/{0}", suggestedContact.Id));
            var connectLink = VirtualPathUtility.ToAbsolute(String.Format("~/user/connect/{0}", suggestedContact.Id));

            html.AppendFormat(@"<a href=""{0}"">{1}</a> - <a href=""{2}"">Connect</a> ", profileLink, suggestedContact.Name, connectLink);

            return new HtmlString(html.ToString());
        }

        private static string RemoveNonNumericChars(string value)
        {
            StringBuilder newValue = new StringBuilder();

            char[] chars = value.ToCharArray();

            foreach (var c in chars)
                if (Char.IsDigit(c))
                    newValue.Append(c);

            return newValue.ToString();
        }

        private static string ConcatenateVisibility(string[] values)
        {
            string value = string.Empty;

            if (values != null)
            {
                if (values.Contains("none"))
                    value = "none";
                else if (values.Contains("everyone"))
                    value = "everyone";
                else
                {
                    foreach (var v in values.Distinct())
                        value += v.ToLower() + ";";
                }
            }

            return value;
        }

        private static string[] SplitVisibility(string value)
        {
            if (!String.IsNullOrWhiteSpace(value))
                return value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            return new string[0];
        }

        private static string FormatVisibility(string value)
        {
            if (!String.IsNullOrWhiteSpace(value))
            {
                string[] values = SplitVisibility(value);

                string formatted = values[0];

                for (int i = 1; i < values.Length; i++)
                {
                    if (i == values.Length - 1)
                        formatted += " and " + values[i];
                    else
                        formatted += ", " + values[i];
                }

                return formatted;
            }

            return "None";
        }
    }
}